//
//  APIManager+requestData.m
//  WithYou
//
//  Created by 牛清旭 on 2020/5/6.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "APIManager+requestData.h"

@implementation APIManager (requestData)
-(NSURLSessionDataTask *)reqestDataWithDict:(NSDictionary *)dict sucess:(void(^)(NSInteger code, NSDictionary* dict))sucess failure:(void(^)(NSInteger code, NSError *error))failure{
    return [self requestWithMethod:HttpRequestTypePost
                         URLString:[NSString stringWithFormat:@"%@%@",BaseUrl,@"login"]
                        parameters:dict
                              done:^(NSURLSessionDataTask *task, id responseObject) {
                                  sucess([self showResponseCode:task],responseObject);
                              } fail:^(NSURLSessionDataTask *task,NSError *error) {
                                  failure([self showResponseCode:task],error);
                              }];
}

- (void)createOrderWithParams:(NSDictionary *)dict success:(void(^)(NSInteger code, NSDictionary *dict))success failure:(void(^)(NSInteger code, NSError *error))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/xml", @"text/plain", @"application/javascript", @"application/x-www-form-urlencoded", @"image/*",@"multipart/form-data", nil];
    NSString *URLString = [NSString stringWithFormat:@"%@%@",BaseUrl,@"image"];
    NSDictionary *userInfo = [DVUserManager shareManager].userInfo;
    NSString *tokens = NULLString(userInfo[kDVUserTokenKey])?@"":userInfo[kDVUserTokenKey];
    NSString *uuid = [FCUUID uuidForDevice]?:@"";
    NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSDictionary *dic = @{@"source":@"app",
                          @"sign":@"",
                          @"version":appVersion,
                          @"app_type":@"ios",
                          @"did":uuid,
                          @"model":[DeviceInfoHander getDeviceInfo],
                          @"token":tokens
    };
    NSLog(@"token===%@",tokens);
    [manager POST:URLString parameters:dict headers:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        UIImage *image = dict[@"img"];
        NSData *imageData = UIImageJPEGRepresentation(image, 0.001);
        [formData appendPartWithFileData:imageData name:@"files" fileName:@"user.png" mimeType:@"image/png"];
        
//        NSData *imageData = [NSData dataWithContentsOfFile:dict[@"files"]];
//        [formData appendPartWithFileData:imageData name:@"files" fileName:@"user.mp4" mimeType:@"application/octet-stream"];
        NSLog(@"%@",formData);
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //打印下上传进度
        NSLog(@"图片上传进度%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",task);
        
        success([self showResponseCode:task],responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([self showResponseCode:task],error);
    }];
    
//    [manager POST:BaseUrl parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//
//        [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//
//        for (int i = 0; i < [dict[@"img"] count]; i ++) {
//            UIImage *image = dict[@"img"][i];
//            NSData *imageData = UIImageJPEGRepresentation(image, 0.001);
////            NSDictionary *paramters = @{@"files": imageData};
//
//            [formData appendPartWithFileData:imageData name:@"files" fileName:@"user.png" mimeType:@"image/png"];
//            NSLog(@"%@",formData);
//        }
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//
//        //打印下上传进度
//        NSLog(@"图片上传进度%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",task);
//
//        success([self showResponseCode:task],responseObject);
//
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//        failure([self showResponseCode:task],error);
//    }];
}

- (void)uploadProductionWithParams:(NSDictionary *)dict success:(void(^)(NSInteger code, NSDictionary *dict))success failure:(void(^)(NSInteger code, NSError *error))failure isVideo:(BOOL)isvideo
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/xml", @"text/plain", @"application/javascript", @"application/x-www-form-urlencoded", @"image/*",@"multipart/form-data", nil];
    NSDictionary *userInfo = [DVUserManager shareManager].userInfo;
    NSString *tokens = NULLString(userInfo[kDVUserTokenKey])?@"":userInfo[kDVUserTokenKey];
    NSString *uuid = [FCUUID uuidForDevice]?:@"";
    NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSDictionary *dic = @{@"source":@"app",
                          @"sign":@"",
                          @"version":appVersion,
                          @"app_type":@"ios",
                          @"did":uuid,
                          @"model":[DeviceInfoHander getDeviceInfo],
                          @"token":tokens
    };
    NSLog(@"token===%@",tokens);
    NSString *URLString = [NSString stringWithFormat:@"%@%@",BaseUrl,@"videoSearch"];
    [manager POST:URLString parameters:dict headers:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        if (isvideo) {
            NSData *imageData = [NSData dataWithContentsOfFile:dict[@"files"]];
            [formData appendPartWithFileData:imageData name:@"files" fileName:@"user.mp4" mimeType:@"application/octet-stream"];
            NSLog(@"%@",formData);
        } else {
            UIImage *image = dict[@"files"][0];
            NSData *imageData = UIImageJPEGRepresentation(image, 0.001);
            //            NSDictionary *paramters = @{@"files": imageData};
                        
            [formData appendPartWithFileData:imageData name:@"files" fileName:@"user.png" mimeType:@"image/png"];
            NSLog(@"%@",formData);
        }
        
                
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //打印下上传进度
        NSLog(@"视频上传进度%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",task);
        
        success([self showResponseCode:task],responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([self showResponseCode:task],error);
    }];
}

- (void)searchWithParams:(NSDictionary *)dict url:(NSString *)url success:(void(^)(NSInteger code, NSDictionary *dict))success failure:(void(^)(NSInteger code, NSError *error))failure isVideo:(BOOL)isvideo
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/xml", @"text/plain", @"application/javascript", @"application/x-www-form-urlencoded", @"image/*",@"multipart/form-data", nil];
    NSDictionary *userInfo = [DVUserManager shareManager].userInfo;
    NSString *tokens = NULLString(userInfo[kDVUserTokenKey])?@"":userInfo[kDVUserTokenKey];
    NSString *uuid = [FCUUID uuidForDevice]?:@"";
    NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSDictionary *dic = @{@"source":@"app",
                          @"sign":@"",
                          @"version":appVersion,
                          @"app_type":@"ios",
                          @"did":uuid,
                          @"model":[DeviceInfoHander getDeviceInfo],
                          @"token":tokens
    };
    NSLog(@"token===%@",tokens);
    NSString *URLString = [NSString stringWithFormat:@"%@%@",BaseUrl,url];
    [manager POST:URLString parameters:dict headers:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        if (isvideo) {
            NSData *imageData = [NSData dataWithContentsOfFile:dict[@"file"]];
            [formData appendPartWithFileData:imageData name:@"file" fileName:@"user.mp4" mimeType:@"application/octet-stream"];
            NSLog(@"%@",formData);
        } else {
            UIImage *image = dict[@"file"];
            NSData *imageData = UIImageJPEGRepresentation(image, 0.001);
            //            NSDictionary *paramters = @{@"files": imageData};
                        
            [formData appendPartWithFileData:imageData name:@"file" fileName:@"user.png" mimeType:@"image/png"];
            NSLog(@"%@",formData);
        }
        
                
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //打印下上传进度
        NSLog(@"视频上传进度%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",task);
        NSString *alertString = NULLString(responseObject[@"message"]) ? @"" : responseObject[@"message"];
        [TipView showMessage:alertString];
        success([self showResponseCode:task],responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([self showResponseCode:task],error);
    }];
}
@end
